package com.example.administrator.nl.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.administrator.nl.R;
import com.example.administrator.nl.activity.LoginActivity;
import com.example.administrator.nl.activity.NoticeActivity;
import com.example.administrator.nl.activity.SearchActivity;
import com.example.administrator.nl.activity.news.NewsActivity;
import com.example.administrator.nl.activity.shop.ShoppingDetailsActivity;
import com.example.administrator.nl.adapter.HomeAdapter;
import com.example.administrator.nl.bean.HomeBean;
import com.example.administrator.nl.util.GlideImageLoader;
import com.example.administrator.nl.util.LogUtil;
import com.example.administrator.nl.util.ToastUtil;
import com.githang.statusbar.StatusBarCompat;
import com.leaf.library.StatusBarUtil;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 首页
 */
public class HomeFragment extends Fragment implements View.OnClickListener {
    private String TAG="HomeFragment";
    private Unbinder unbinder;


//    @BindView(R.id.banner_home)
//    Banner mBanner;

    @BindView(R.id.home_recycle_view)
    RecyclerView home_recycle_view;

    //    @BindView(R.id.ll_home)
//    LinearLayout ll_home;
    List<HomeBean> list;
    private HomeAdapter homeAdapter;

    // 头布局
    View headView;

    Banner banner_home;
    RelativeLayout rl_notice_home;
    LinearLayout ll_search_home;
    EditText et_search_home;
    List<Integer> listImages;

    ImageView iv_news_home;

    TextView tv_news_home;


    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        unbinder = ButterKnife.bind(this, view);
        LogUtil.Log_D(TAG,"onCreateView");
        ToastUtil.init(getActivity());

        // 设置状态栏颜色
        init();
        listener();
        return view;
    }

    private void listener() {
        homeAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.ll_shopping:
                        Intent intent = new Intent(getActivity(), ShoppingDetailsActivity.class);
                        startActivity(intent);
                        break;
                }
            }
        });
        rl_notice_home.setOnClickListener(this);
        ll_search_home.setOnClickListener(this);
        et_search_home.setOnClickListener(this);
        iv_news_home.setOnClickListener(this);
        tv_news_home.setOnClickListener(this);

    }

    private void init() {
        // 头布局
        headView = LayoutInflater.from(getActivity()).inflate(R.layout.banner_home_layout, null);
        banner_home = headView.findViewById(R.id.banner_home);
        rl_notice_home = headView.findViewById(R.id.rl_notice_home);
        ll_search_home = headView.findViewById(R.id.ll_search_home);
        et_search_home = headView.findViewById(R.id.et_search_home);
        iv_news_home = headView.findViewById(R.id.iv_news_home);
        tv_news_home = headView.findViewById(R.id.tv_news_home);
//        StatusBarUtil.setColor(getActivity(), getResources().getColor(R.color.blue_60d1f7));
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
        home_recycle_view.setLayoutManager(gridLayoutManager);
        list = getData();
        homeAdapter = new HomeAdapter(R.layout.item_home_layout, list);
        homeAdapter.setHeaderView(headView);

        banner_home.setImageLoader(new GlideImageLoader());
        listImages = new ArrayList<>();
        listImages.add(R.mipmap.ic_banner);
        listImages.add(R.mipmap.ic_banner);
        listImages.add(R.mipmap.ic_banner);
        banner_home.setImages(listImages);
        banner_home.setBannerAnimation(Transformer.CubeIn);
        banner_home.setBannerStyle(BannerConfig.NOT_INDICATOR);

        //设置自动轮播，默认为true
        banner_home.isAutoPlay(true);
        //设置轮播时间
        banner_home.setDelayTime(3000);
        //banner设置方法全部调用完毕时最后调用
        banner_home.start();
        home_recycle_view.setAdapter(homeAdapter);

    }


    public List<HomeBean> getData() {
        List<HomeBean> list = new ArrayList<>();
        list.add(new HomeBean());
        list.add(new HomeBean());
        list.add(new HomeBean());
        list.add(new HomeBean());
        list.add(new HomeBean());
        list.add(new HomeBean());
        list.add(new HomeBean());
        list.add(new HomeBean());
        list.add(new HomeBean());
        list.add(new HomeBean());
        list.add(new HomeBean());
        return list;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_notice_home:
                startActivity(new Intent(getActivity(), NoticeActivity.class));
                break;
            case R.id.ll_search_home:
            case R.id.et_search_home:
                startActivity(new Intent(getActivity(), SearchActivity.class));
                break;

            case R.id.tv_news_home:
            case R.id.iv_news_home:
                startActivity(new Intent(getActivity(), NewsActivity.class));
                break;
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        LogUtil.Log_D(TAG,"onStart");

        if(null != banner_home){
            banner_home.startAutoPlay();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        LogUtil.Log_D(TAG,"onPause");

        if(null != banner_home){
            banner_home.stopAutoPlay();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.Log_D(TAG,"onDestroy");
        if (null != unbinder) {
            unbinder.unbind();
            unbinder = null;
        }
    }
}
